package com.cssw.fyzb.core.mao.live;

import com.cssw.fyzb.core.model.mos.AudienceMo;
import com.cssw.fyzb.core.model.mos.GiftNumMo;
import com.cssw.fyzb.core.model.mos.UserLiveMo;
import com.cssw.fyzb.core.mongo.MongoClient;
import com.cssw.fyzb.core.util.DocUtil;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @author Feng Chen
 */
@Component
public class UserLiveMaoImpl implements UserLiveMao {

    @Autowired
    private MongoClient mongoClient;

    @Override
    public UserLiveMo findByLiveId(Long liveId) {
        Document doc = mongoClient.getCollection("user_live")
                .find(new Document("liveId", liveId))
                .first();
        return DocUtil.decoce(doc, UserLiveMo.class);
    }

    @Override
    public void create(Long liveId) {
        UserLiveMo userLiveMo = new UserLiveMo();
        userLiveMo.setLiveId(liveId);
        userLiveMo.setAudiences(Collections.emptyList());
        userLiveMo.setGifts(Collections.emptyList());
        mongoClient.getCollection("user_live").insertOne(DocUtil.encode(userLiveMo));
    }

    @Override
    public boolean exists(Long liveId, Long userId) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("audiences.userId", userId);

        Document doc = mongoClient.getCollection("user_live")
                .find(query)
                .first();

        return doc != null;
    }

    @Override
    public void enter(Long liveId, Long userId) {
        AudienceMo audience = new AudienceMo();
        audience.setUserId(userId);
        audience.setState("1");
        audience.setReward(0);
        mongoClient.getCollection("user_live").updateOne(
                new Document("liveId", liveId),
                new Document().append("$push", new Document("audiences", DocUtil.encode(audience)))
        );
    }

    @Override
    public void reEnter(Long liveId, Long userId) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("audiences.userId", userId);
        Document set = new Document()
                .append("$set", new Document().append("audiences.$.state", "1"));
        mongoClient.getCollection("user_live").updateOne(query, set);
    }

    @Override
    public void exit(Long liveId, Long userId) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("audiences.userId", userId);
        Document set = new Document()
                .append("$set", new Document().append("audiences.$.state", "0"));
        mongoClient.getCollection("user_live").updateOne(query, set);
    }

    @Override
    public void incReward(Long liveId, Long userId, Integer reward) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("audiences.userId", userId);
        Document set = new Document()
                .append("$inc", new Document().append("audiences.$.reward", reward));
        mongoClient.getCollection("user_live").updateOne(query, set);
    }

    @Override
    public boolean existsGift(Long liveId, Long giftId) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("gifts.giftId", giftId);

        Document doc = mongoClient.getCollection("user_live")
                .find(query)
                .first();

        return doc != null;
    }


    @Override
    public void incGift(Long liveId, Long giftId) {
        Document query = new Document()
                .append("liveId", liveId)
                .append("gifts.giftId", giftId);
        Document set = new Document()
                .append("$inc", new Document().append("gifts.$.num", 1));
        mongoClient.getCollection("user_live").updateOne(query, set);
    }

    @Override
    public void saveGift(Long liveId, Long giftId) {
        GiftNumMo giftNum = new GiftNumMo();
        giftNum.setGiftId(giftId);
        giftNum.setNum(1);
        mongoClient.getCollection("user_live").updateOne(
                new Document("liveId", liveId),
                new Document().append("$push", new Document("gifts", DocUtil.encode(giftNum)))
        );
    }
}
